package com.blackshadowgame.utils;

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */

//package com.huawei.developerguide.utils;

//import com.example.myace.loginstate.DistributedLoginState;

//import com.huawei.developerguide.moudle_imail.bean.PictureData;
//import com.huawei.developerguide.ui.WidgetHelper;
import ohos.account.AccountAbility;
import ohos.account.DistributedInfo;
import ohos.app.AbilityContext;
import ohos.app.Context;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

//import ohos.idn.DistributedNetAdapter;
//import ohos.idn.IdnDeviceInfo;

/**
 * Device method utils
 *
 * @since 2020-01-17
 */
public class DeviceUtils {
    private static final String TAG = DeviceUtils.class.getSimpleName();

//    private static final DistributedNetAdapter sDistributedNetAdapter = DistributedNetAdapter.getInstance();

//    private static final DistributedLoginState sDistributedLoginStateinstance = DistributedLoginState.getInstance();

    private DeviceUtils() {
        // do nothing
    }

    /**
     * Get group id.
     *
     * @return group id.
     * @since 2020-01-12
     */
    public static String getGroupId() {
        AccountAbility account = AccountAbility.getAccountAbility();
        DistributedInfo distributeInfo = account.queryOsAccountDistributedInfo();
        return distributeInfo.getId();
    }

    public static String getFirstDeviceId(){
        String deviceId = "";
        List<String> outerDevices = DeviceUtils.getAvailableDeviceId();
        LogUtil.info(TAG, "getDeviceId DeviceUtils.getRemoteDevice() = " + outerDevices);
        if (outerDevices == null || outerDevices.size() == 0){
            LogUtil.error(TAG, "no other device to continue");
        }else {
            for (String item : outerDevices) {
                LogUtil.info(TAG, "item deviceId = " + item);
            }
            deviceId = outerDevices.get(0);
        }
        LogUtil.info(TAG, "continueAbility to deviceId = " + deviceId);
        return deviceId;
    };

    public static List<DeviceInfo> getAllDevices(){
        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        return deviceInfoList;
    }

    public static DeviceInfo getFirstDevice(){
        LogUtil.info(TAG, "begin getAvailableDeviceId");
//        List<String> deviceIds = new ArrayList<>();
//        List<IdnDeviceInfo> deviceInfoList =
//            sDistributedNetAdapter.getDeviceInfoList(DistributedNetAdapter.FLAG_ONLINE);

        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        if (deviceInfoList == null || deviceInfoList.size()<=0) {
            return null;
        }
        for (DeviceInfo deviceInfo : deviceInfoList) {
            LogUtil.info(TAG, "==> Online Device: " + deviceInfo.getDeviceName());
        }

        LogUtil.info(TAG, "deviceInfoList size " + deviceInfoList.size());

        return deviceInfoList.get(0);
    }

    /**
     * Get available id but not contain self.
     *
     * @return available device ids
     * @since 2020-01-12
     */
    public static List<String> getAvailableDeviceId() {
        LogUtil.info(TAG, "begin getAvailableDeviceId");
        List<String> deviceIds = new ArrayList<>();
//        List<IdnDeviceInfo> deviceInfoList =
//            sDistributedNetAdapter.getDeviceInfoList(DistributedNetAdapter.FLAG_ONLINE);

        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        if (deviceInfoList == null) {
            return deviceIds;
        }
        LogUtil.info(TAG, "deviceInfoList size " + deviceInfoList.size());
//        String localDeviceId = getLocalDeviceId();
        if (deviceInfoList.size() == 0) {
//        if (deviceInfoList.size() == 0 || !sDistributedLoginStateinstance.isLoginIn(localDeviceId)) {
            LogUtil.info(TAG, "did not find other device");
            return deviceIds;
        }

        for (DeviceInfo deviceInfo : deviceInfoList) {
            deviceIds.add(deviceInfo.getDeviceId());
//            LogUtil.info(TAG, "==> Get Device: " + deviceInfo.getDeviceName());
//            if (!sDistributedLoginStateinstance.isLoginIn(deviceInfo.getNodeId())) {
//                LogUtil.info(TAG, deviceInfo.getNodeId() + " did not login");
//                continue;
//            } else if (deviceInfo.getNodeId().equals(localDeviceId)) {
//                LogUtil.info(TAG, "skip local device");
//                continue;
//            } else {
//                deviceIds.add(deviceInfo.getNodeId());
//            }
        }
        LogUtil.info(TAG, "deviceInfoList end.... ");
        return deviceIds;
    }

    /**
     * Get available id
     *
     * @return available device ids
     * @since 2020-01-12
     */
    public static List<String> getAllAvailableDeviceId() {
//        LogUtil.info(TAG, "begin getAllAvailableDeviceId");
        List<String> deviceIds = new ArrayList<>();
////        List<IdnDeviceInfo> deviceInfoList =
////            sDistributedNetAdapter.getDeviceInfoList(DistributedNetAdapter.FLAG_ONLINE);
//        List<BasicInfo> deviceInfoList = sDeviceManager.getNodesBasicInfo();
//        if (deviceInfoList == null) {
//            return deviceIds;
//        }
//        LogUtil.info(TAG, "deviceInfoList size " + deviceInfoList.size());
//        if (deviceInfoList.size() > 0) {
//            for (BasicInfo deviceInfo : deviceInfoList) {
//                if (!sDistributedLoginStateinstance.isLoginIn(deviceInfo.getNodeId())) {
//                    LogUtil.info(TAG, deviceInfo.getNodeId() + " did not login");
//                    continue;
//                } else {
//                    deviceIds.add(deviceInfo.getNodeId());
//                }/
//            }
//        }
        return deviceIds;
    }

    /**
     * Get remote device info
     *
     * @return Remote device info list.
     */
    public static List<DeviceInfo> getRemoteDevice() {
//        String localDeviceId = getLocalDeviceId();

//        List<IdnDeviceInfo> deviceInfos = sDistributedNetAdapter.getDeviceInfoList(DistributedNetAdapter.FLAG_ONLINE);
        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
//        Iterator<BasicInfo> iterator = deviceInfoList.iterator();
//        while (iterator.hasNext()) {
//            BasicInfo deviceInfo = iterator.next();
//            if (!sDistributedLoginStateinstance.isLoginIn(deviceInfo.getNodeId())) {
//                iterator.remove();
//                continue;
//            }
//            if (localDeviceId.equals(deviceInfo.getNodeId())) {
//                iterator.remove();
//                continue;
//            }
//        }

        return deviceInfoList;
    }

//    // 查询已经启动某个服务的设备
//    public static DeviceInfo getDeviceWithService(){
//        DeviceInfo deviceInfo = null;
//
//        DeviceManager.queryRemoteAbilityByIntent()
//
//        return deviceInfo;
//    }

    /**
     * Get local device id.
     *
     * @return local device id
     * @since 2020-01-12
     */
//    public static String getLocalDeviceId() {
//        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE );;
////        Optional<String> deviceId = sDistributedNetAdapter.getLocalDeviceId();
//        Optional<BasicInfo> deviceId = sDeviceManager.getLocalBasicInfo();
//        if (deviceId.isPresent()) {
//            return deviceId.get().getNodeId();
//        } else {
//            LogUtil.error(TAG, "get local device id failure.");
//            return "";
//        }
//    }

//    public static boolean checkDevices(List<Contact> contacts) {
//        List<String> deviceIds = getAllAvailableDeviceId();
//        for (Contact contact : contacts) {
//            boolean deviceOnline = false;
//            for (String deviceId : deviceIds) {
//                if (deviceId.equals(contact.getDeviceId())) {
//                    deviceOnline = true;
//                    break;
//                }
//            }
//            if (!deviceOnline) {
//                return false;
//            }
//        }
//        return true;
//    }
//
//    /**
//     * Get local device picture.
//     * tip：picture文件名包含设备类型
//     *
//     * @return local device picture
//     * @since 2020-11-04
//     */
//    public static List<PictureData> getLocalFile(AbilityContext context) {
//        if (context.getDistributedDir() == null) {
//            WidgetHelper.showTips(context, "分布式异常！");
//            return new ArrayList<>();
//        }
//        File file = new File(context.getDistributedDir().getPath());
//        File[] files = file.listFiles();
//        if (files == null) {
//            LogUtil.error("error", "空目录");
//            return new ArrayList<>();
//        }
//        List<PictureData> list = new ArrayList<>();
//        for (int i = 0; i < files.length; i++) {
//            String fileName = files[i].getAbsolutePath().substring(files[i].getAbsolutePath().lastIndexOf("/") + 1);
//            LogUtil.debug(TAG, fileName + "==" + files[i].getPath() + "==" + files[i].getAbsolutePath());
//            if (fileName.contains(ohos.system.DeviceInfo.getName())) {
//                list.add(new PictureData(fileName.split("@")[1], files[i].getPath()));
//            }
//        }
//        return list;
//    }
//
//    /**
//     * Get Remote device picture.
//     * tip：picture文件名包含设备类型
//     *
//     * @return Remote device picture
//     * @since 2020-11-04
//     */
//    public static List<PictureData> getRemoteFile(AbilityContext context) {
//        if (context.getDistributedDir() == null) {
//            WidgetHelper.showTips(context, "分布式异常！");
//            return new ArrayList<>();
//        }
//        File file = new File(context.getDistributedDir().getPath());
//        File[] files = file.listFiles();
//        if (files == null) {
//            LogUtil.error("error", "空目录");
//            return new ArrayList<>();
//        }
//        List<PictureData> list = new ArrayList<>();
//        for (int i = 0; i < files.length; i++) {
//            String fileName = files[i].getAbsolutePath().substring(files[i].getAbsolutePath().lastIndexOf("/") + 1);
//            LogUtil.debug(TAG, fileName + "==" + files[i].getPath() + "==" + files[i].getAbsolutePath());
//            if (!fileName.contains(ohos.system.DeviceInfo.getName())) {
//                list.add(new PictureData(fileName.split("@")[1], files[i].getPath()));
//            }
//        }
//        return list;
//    }
}
